package com.example.boot06.controller;

import com.example.boot06.entity.User;
import com.example.boot06.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;

    @RequestMapping("/reg")
    public int reg(@RequestBody User user){
        System.out.println("user = " + user);
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            /* 用户名已存在 */
            return 2;
        }
        mapper.insert(user);
        /* 代表注册成功 */
        return 1;
    }

    @RequestMapping("/login")
    public int login(@RequestBody User user){
        User u = mapper.selectByUsername(user.getUsername());
        if (u!=null){
            if (u.getPassword().equals(user.getPassword())) {
                /* 登录成功 */
                return 1;
            }
            /* 密码错误 */
            return 2;
        }
        /* 用户名不存在 */
        return 3;
    }
}
